import { notification } from 'antd';
import { queryUploadImgConfig } from '@/services/upload';
import { uploadImg, uploadApp, uploadAny } from '@/utils/upload';

export default {
  namespace: 'upload',

  state: {
    imgValue: {
      url: null,
      message: null
    },
    fileValue: {
      url: null,
      message: null
    },
    filesValue: {
      data: [],
      message: null
    }
  },

  effects: {
    *uploadImg({ payload }, { call, put }) {
      const config = yield call(queryUploadImgConfig);
      yield put({ type: 'setImageConfig', payload: config });
      if(payload) {
        const img = uploadImg(payload, config);
        yield put({
          type: 'setImgValue',
          payload: {
            url: img,
            message: img ? 'success' : 'fail'
          }
        })
      }else {
        yield put({
          type: 'setImgValue',
          payload: {
            url: '',
            message: ''
          }
        })
      }
    },
    *uploadFile({ payload }, { call, put }) {
      const config = yield call(queryUploadImgConfig);
      yield put({ type: 'setImageConfig', payload: config })
      const file = uploadApp(payload, config);
      yield put({
        type: 'setFileValue',
        payload: {
          url: file,
          message: file ? 'success' : 'fail'
        }
      })
      file && notification.success({
        message: '保存成功',
        description: '导入文件成功'
      });
    },
    *uploadAny({ payload }, { call, put }) {
      const config = yield call(queryUploadImgConfig);
      yield put({ type: 'setImageConfig', payload: config })
      const file = uploadAny(payload, config);
      yield put({
        type: 'setFileValue',
        payload: {
          url: file,
          message: file ? 'success' : 'fail'
        }
      })
      file && notification.success({
        message: '保存成功',
        description: '导入文件成功'
      });
    },
    *uploadAll({ payload }, { call, put, all }) {
      const config = yield call(queryUploadImgConfig);
      yield put({ type: 'setImageConfig', payload: config });

      const ans = yield Promise.all(payload.map(v => uploadAny(v, config)))
      yield put({ type: 'setFilesValue', payload: {
        data: ans,
        message: Array.isArray(ans) && ans.length === payload.length
      } })
    },
    *init(_, { put }) {
      yield put({type: 'setInit'})
    }
  },

  reducers: {
    setImageConfig(state, action) {
      return {
        ...state,
        image: action.payload
      }
    },
    setImgValue(state, action) {
      return {
        ...state,
        imgValue: action.payload
      }
    },
    setFileValue(state, action) {
      return {
        ...state,
        fileValue: action.payload
      }
    },
    setFilesValue(state, action) {
      return {
        ...state,
        filesValue: action.payload
      }
    },
    setInit(state) {
      return {
        ...state,
        image: null,
        imgValue: {
          url: null,
          message: null
        },
        fileValue: {
          url: null,
          message: null
        },
        files: {
          data: [],
          message: null
        }
      }
    }
  },
};